package adapter;

import Util.TConfig;
import Util.Util;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thanhnx.tlg1.MyApp;
import com.thanhnx.tlg1.R;

public class DataAdapter extends BaseAdapter {

	private int item_h;
	private int item_w;
	private int item_h_100;
	private int item_w_100;
	private int item_h_unit;// 5 dips
	private int MAX;
	private int MIN;
	private final Activity context;
	private String[][] datas;
	private boolean isZoomViewHolder = false; // determine whether to change the
												// viewholder object for zoom

	public DataAdapter(Activity context, String[][] datas) {
		this.context = context;
		this.datas = datas;
		this.item_h = Util.convertDpToPixel(20, context);
		this.item_w = Util.convertDpToPixel(1080 + 27, context);
		this.item_h_unit = Util.convertDpToPixel(5, context);
		this.MIN = 3 * this.item_h_unit;// 15dip
		this.MAX = 16 * this.item_h_unit;// 80dip
		this.item_h_100 = this.item_h;
		this.item_w_100 = this.item_w;
	}

	public int requestZoom(int progress) {
		float x = (float) (progress / 100.0);
		item_h = (int) (x * item_h_100);
		item_w = (int) (x * item_w_100);
		isZoomViewHolder = true;
		return item_w;
	}

	public int requestBiggerRowX(int x) {
		item_h = (int) (item_h_100 + TConfig.zoom_ratio * x * item_h_100);
		item_w = (int) (item_w_100 + TConfig.zoom_ratio * x * item_w_100);
		isZoomViewHolder = true;
		return item_w;
	}

	public int requestSmallerRowX(int x) {
		item_h = (int) (item_h_100 - TConfig.zoom_ratio * x * item_h_100);
		item_w = (int) (item_w_100 - TConfig.zoom_ratio * x * item_w_100);
		isZoomViewHolder = true;
		return item_w;
	}

	public int requestBiggerRow() {
		if (item_h + TConfig.zoom_ratio * item_h < MAX) {
			item_h = (int) (item_h + TConfig.zoom_ratio * item_h);
			item_w = (int) (item_w + TConfig.zoom_ratio * item_w);
			isZoomViewHolder = true;
			// notifyDataSetChanged();
			return item_w;
		} else {
			return item_w;
		}
	}

	public int requestBiggerRowPlus() {
		if (item_h + TConfig.zoom_ratio_plus * item_h < MAX) {
			item_h = (int) (item_h + TConfig.zoom_ratio_plus * item_h);
			item_w = (int) (item_w + TConfig.zoom_ratio_plus * item_w);
			isZoomViewHolder = true;
			// notifyDataSetChanged();
			return item_w;
		} else {
			return 0;
		}

	}

	public int requestSmallerRow() {
		if (item_h - TConfig.zoom_ratio * item_h > MIN) {
			item_h = (int) (item_h - TConfig.zoom_ratio * item_h);
			item_w = (int) (item_w - TConfig.zoom_ratio * item_w);
			isZoomViewHolder = true;
			// notifyDataSetChanged();
			return item_w;
		} else {
			return 0;
		}
	}

	public int requestSmallerRowPlus() {
		if (item_h - TConfig.zoom_ratio_plus * item_h > MIN) {
			item_h = (int) (item_h - TConfig.zoom_ratio_plus * item_h);
			item_w = (int) (item_w - TConfig.zoom_ratio_plus * item_w);
			isZoomViewHolder = true;
			// notifyDataSetChanged();
			return item_w;
		} else {
			return 0;
		}
	}

	@Override
	public int getCount() {
		return MyApp.MAX_ROW;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	static class ViewHolder {
		protected LinearLayout ll_root;
		// module 1
		protected TextView tv_0;
		protected TextView tv_1;
		protected TextView tv_2;
		protected TextView tv_3;
		protected TextView tv_4;
		protected TextView tv_5;
		protected TextView tv_6;
		protected TextView tv_7;
		protected TextView tv_8;
		// module 2
		protected TextView tv_9;
		protected TextView tv_10;
		protected TextView tv_11;
		protected TextView tv_12;
		protected TextView tv_13;
		protected TextView tv_14;
		protected TextView tv_15;
		protected TextView tv_16;
		protected TextView tv_17;
		// module 3
		protected TextView tv_18;
		protected TextView tv_19;
		protected TextView tv_20;
		protected TextView tv_21;
		protected TextView tv_22;
		protected TextView tv_23;
		protected TextView tv_24;
		protected TextView tv_25;
		protected TextView tv_26;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parrent) {
		Log.e("getView", "position = " + position);
		ViewHolder holder;
		if (convertView == null) {
			Log.e("getView", "position = " + position + " convertView == null");
			LayoutInflater inflator = context.getLayoutInflater();
			convertView = inflator.inflate(R.layout.item_data, null);
			holder = new ViewHolder();
			// ll_root
			holder.ll_root = (LinearLayout) convertView
					.findViewById(R.id.ll_root);
			holder.ll_root.setLayoutParams(new AbsListView.LayoutParams(item_w,
					item_h));
			// Module 1
			holder.tv_0 = (TextView) convertView.findViewById(R.id.item_0);
			holder.tv_1 = (TextView) convertView.findViewById(R.id.item_1);
			holder.tv_2 = (TextView) convertView.findViewById(R.id.item_2);
			holder.tv_3 = (TextView) convertView.findViewById(R.id.item_3);
			holder.tv_4 = (TextView) convertView.findViewById(R.id.item_4);
			holder.tv_5 = (TextView) convertView.findViewById(R.id.item_5);
			holder.tv_6 = (TextView) convertView.findViewById(R.id.item_6);
			holder.tv_7 = (TextView) convertView.findViewById(R.id.item_7);
			holder.tv_8 = (TextView) convertView.findViewById(R.id.item_8);
			// Module 2
			holder.tv_9 = (TextView) convertView.findViewById(R.id.item_9);
			holder.tv_10 = (TextView) convertView.findViewById(R.id.item_10);
			holder.tv_11 = (TextView) convertView.findViewById(R.id.item_11);
			holder.tv_12 = (TextView) convertView.findViewById(R.id.item_12);
			holder.tv_13 = (TextView) convertView.findViewById(R.id.item_13);
			holder.tv_14 = (TextView) convertView.findViewById(R.id.item_14);
			holder.tv_15 = (TextView) convertView.findViewById(R.id.item_15);
			holder.tv_16 = (TextView) convertView.findViewById(R.id.item_16);
			holder.tv_17 = (TextView) convertView.findViewById(R.id.item_17);
			// Module 3
			holder.tv_18 = (TextView) convertView.findViewById(R.id.item_18);
			holder.tv_19 = (TextView) convertView.findViewById(R.id.item_19);
			holder.tv_20 = (TextView) convertView.findViewById(R.id.item_20);
			holder.tv_21 = (TextView) convertView.findViewById(R.id.item_21);
			holder.tv_22 = (TextView) convertView.findViewById(R.id.item_22);
			holder.tv_23 = (TextView) convertView.findViewById(R.id.item_23);
			holder.tv_24 = (TextView) convertView.findViewById(R.id.item_24);
			holder.tv_25 = (TextView) convertView.findViewById(R.id.item_25);
			holder.tv_26 = (TextView) convertView.findViewById(R.id.item_26);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.ll_root.setLayoutParams(new AbsListView.LayoutParams(item_w,
					item_h));
			// if (isZoomViewHolder) {
			// Log.e("getView", "position = " + position
			// + " isZoomViewHolder = TRUE");
			// holder.ll_root.setLayoutParams(new AbsListView.LayoutParams(
			// item_w, item_h));
			// convertView.setTag(holder);
			// isZoomViewHolder = false;
			// }
		}
		// module 1
		holder.tv_0.setText(datas[position][0]);
		holder.tv_1.setText(datas[position][1]);
		holder.tv_2.setText(datas[position][2]);
		holder.tv_3.setText(datas[position][3]);
		holder.tv_4.setText(datas[position][4]);
		holder.tv_5.setText(datas[position][5]);
		holder.tv_6.setText(datas[position][6]);
		holder.tv_7.setText(datas[position][7]);
		holder.tv_8.setText(datas[position][8]);
		// module 2
		holder.tv_9.setText(datas[position][9]);
		holder.tv_10.setText(datas[position][10]);
		holder.tv_11.setText(datas[position][11]);
		holder.tv_12.setText(datas[position][12]);
		holder.tv_13.setText(datas[position][13]);
		holder.tv_14.setText(datas[position][14]);
		holder.tv_15.setText(datas[position][15]);
		holder.tv_16.setText(datas[position][16]);
		holder.tv_17.setText(datas[position][17]);
		// module 3
		holder.tv_18.setText(datas[position][18]);
		holder.tv_19.setText(datas[position][19]);
		holder.tv_20.setText(datas[position][20]);
		holder.tv_21.setText(datas[position][21]);
		holder.tv_22.setText(datas[position][22]);
		holder.tv_23.setText(datas[position][23]);
		holder.tv_24.setText(datas[position][24]);
		holder.tv_25.setText(datas[position][25]);
		holder.tv_26.setText(datas[position][26]);
		return convertView;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}
}
